//
//  IntegralBouncedView.m
//  ZQLearn
//
//  Created by lipei on 2019/6/12.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "IntegralBouncedView.h"

@implementation IntegralBouncedView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        self.bageImageView = [[UIImageView alloc]init];
        self.bageImageView.image = [UIImage imageNamed:@"tuceng"];
        self.bageImageView.userInteractionEnabled = YES;
        [self addSubview:self.bageImageView];
        [self.bageImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.top.equalTo(self);
            make.bottom.equalTo(self);
        }];
        [self.bageImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissAlert)]];
        
        self.integralView = [[UIView alloc] init];
        self.integralView.backgroundColor = kWhiteColor;
        self.integralView.layer.cornerRadius = 8;
        self.integralView.layer.masksToBounds = YES;
        [self addSubview:self.integralView];
        [self.integralView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.mas_equalTo(ScreenWidth-50);
            make.centerY.equalTo(self).offset(-10);
            make.height.mas_equalTo(250);
        }];
        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.textColor = kBlackColor;
        self.titleLabel.font = [UIFont systemFontOfSize:18];
        self.titleLabel.text = @"选择积分抵扣金额";
        [self.integralView addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.integralView);
            make.top.mas_equalTo(20);
        }];
        self.lineView = [[UIView alloc] init];
        self.lineView.backgroundColor = [UIColor colorWithHex:@"efefef"];
        [self.integralView addSubview:self.lineView];
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.integralView);
            make.width.mas_equalTo(ScreenWidth-130);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
            make.height.mas_equalTo(1);
        }];
        self.integralTitleLabel = [[UILabel alloc]init];
        self.integralTitleLabel.textColor = [UIColor colorWithHex:@"333333"];
        self.integralTitleLabel.font = [UIFont systemFontOfSize:15];
        self.integralTitleLabel.text = @"我的积分:";
        [self.integralView addSubview:self.integralTitleLabel];
        [self.integralTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.lineView.mas_left);
            make.top.equalTo(self.lineView.mas_bottom).offset(40);
        }];
        self.integralLabel = [[UILabel alloc]init];
        self.integralLabel.textColor = [UIColor colorWithHex:@"333333"];
        self.integralLabel.font = [UIFont systemFontOfSize:16];
        self.integralLabel.text = [NSString stringWithFormat:@"%@",[UserInfoSave shareInstance].integral];
        self.strIntegral = [NSString stringWithFormat:@"%@",[UserInfoSave shareInstance].integral];
        [self.integralView addSubview:self.integralLabel];
        [self.integralLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.integralTitleLabel.mas_right).offset(5);
            make.centerY.equalTo(self.integralTitleLabel);
        }];
        self.amountTitleLabel = [[UILabel alloc]init];
        self.amountTitleLabel.textColor = [UIColor colorWithHex:@"333333"];
        self.amountTitleLabel.font = [UIFont systemFontOfSize:15];
        self.amountTitleLabel.text = @"待支付金额:";
        [self.integralView addSubview:self.amountTitleLabel];
        [self.amountTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.lineView.mas_left);
            make.top.equalTo(self.integralTitleLabel.mas_bottom).offset(10);
        }];
        self.symbolLabel = [[UILabel alloc]init];
        self.symbolLabel.textColor = [UIColor colorWithHex:@"fa4247"];
        self.symbolLabel.font = [UIFont systemFontOfSize:16];
        self.symbolLabel.text = @"¥";
        [self.integralView addSubview:self.symbolLabel];
        [self.symbolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.amountTitleLabel.mas_right).offset(5);
            make.centerY.equalTo(self.amountTitleLabel);
        }];
        self.amountLabel = [[UILabel alloc]init];
        self.amountLabel.textColor = [UIColor colorWithHex:@"fa4247"];
//        self.amountLabel.textColor = kMagentaColor;
        self.amountLabel.font = [UIFont systemFontOfSize:16];
        self.amountLabel.text = @"0";
        [self.integralView addSubview:self.amountLabel];
        [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.symbolLabel.mas_right).offset(2);
            make.centerY.equalTo(self.amountTitleLabel);
        }];
        self.promptLabel = [[UILabel alloc]init];
        self.promptLabel.textColor = [UIColor colorWithHex:@"333333"];
        self.promptLabel.font = [UIFont systemFontOfSize:15];
        self.promptLabel.text = @"(每100积分可抵扣1元)";
        [self.integralView addSubview:self.promptLabel];
        [self.promptLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.lineView.mas_left);
            make.top.equalTo(self.amountTitleLabel.mas_bottom).offset(45);
        }];
        self.addNumButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.addNumButton setTitle:@"+" forState:UIControlStateNormal];
        self.addNumButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.addNumButton setTitleColor:kBlackColor forState:UIControlStateNormal];
        self.addNumButton.tag = 100;
        self.addNumButton.layer.borderColor = [UIColor colorWithHex:@"999999"].CGColor;
        self.addNumButton.layer.borderWidth = 1;
        [self.addNumButton setBackgroundColor:kWhiteColor];
        [self.integralView addSubview:self.addNumButton];
        [self.addNumButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.lineView.mas_right);
            make.width.mas_equalTo(40);
            make.centerY.equalTo(self.amountTitleLabel);
            make.height.mas_equalTo(40);
        }];
        [self.addNumButton addTarget:self action:@selector(amountAction:) forControlEvents:UIControlEventTouchUpInside];
        self.numberLabel = [[UILabel alloc]init];
        self.numberLabel.textColor = kBlackColor;
        self.numberLabel.font = [UIFont systemFontOfSize:18];
        self.numberLabel.text = @"0";
        self.numberLabel.textAlignment = NSTextAlignmentCenter;
        [self.integralView addSubview:self.numberLabel];
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.addNumButton.mas_left);
            make.centerY.equalTo(self.addNumButton);
            make.width.mas_equalTo(40);
        }];
        self.cutNumButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.cutNumButton setTitle:@"-" forState:UIControlStateNormal];
        self.cutNumButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.cutNumButton setTitleColor:kBlackColor forState:UIControlStateNormal];
        self.cutNumButton.tag = 101;
        [self.cutNumButton setBackgroundColor:kWhiteColor];
        self.cutNumButton.layer.borderColor = [UIColor colorWithHex:@"999999"].CGColor;
        self.cutNumButton.layer.borderWidth = 1;
        [self.integralView addSubview:self.cutNumButton];
        [self.cutNumButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.numberLabel.mas_left);
            make.width.mas_equalTo(40);
            make.centerY.equalTo(self.addNumButton);
            make.height.mas_equalTo(40);
        }];
        [self.cutNumButton addTarget:self action:@selector(amountAction:) forControlEvents:UIControlEventTouchUpInside];
        self.topLineView = [[UIView alloc] init];
        self.topLineView.backgroundColor = [UIColor colorWithHex:@"999999"];
        [self.integralView addSubview:self.topLineView];
        [self.topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.addNumButton.mas_bottom);
            make.right.equalTo(self.addNumButton.mas_left);
            make.left.equalTo(self.cutNumButton.mas_right);
            make.height.mas_equalTo(1);
        }];
        self.bottomLineView = [[UIView alloc] init];
        self.bottomLineView.backgroundColor = [UIColor colorWithHex:@"999999"];
        [self.integralView addSubview:self.bottomLineView];
        [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.addNumButton.mas_top);
            make.right.equalTo(self.addNumButton.mas_left);
            make.left.equalTo(self.cutNumButton.mas_right);
            make.height.mas_equalTo(1);
        }];
        self.payButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.payButton setTitle:@"去支付" forState:UIControlStateNormal];
        self.payButton.titleLabel.font = [UIFont systemFontOfSize:18];
        [self.payButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
        self.payButton.tag = 100;
//        [self.payButton setBackgroundColor:[UIColor colorWithHex:@"5db2ff"]];
        self.payButton.backgroundColor = ThemeBlue;
        self.payButton.layer.cornerRadius = 8;
        self.payButton.layer.masksToBounds = YES;
        [self.integralView addSubview:self.payButton];
        [self.payButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.promptLabel.mas_right).offset(20);
            make.right.equalTo(self.lineView.mas_right);
            make.centerY.equalTo(self.promptLabel);
            make.height.mas_equalTo(40);
        }];
        [self.payButton addTarget:self action:@selector(payClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
- (void)amountAction:(UIButton *)btn{
    NSInteger integer = [self.strIntegral integerValue]/100;
//    NSInteger remainder = [self.integralLabel.text integerValue]%100;
    
    if (btn.tag == 100) {//+
        if ([self.numberLabel.text integerValue] >= integer) {
            return;
        } else {
            self.numberLabel.text = [NSString stringWithFormat:@"%ld",[self.numberLabel.text integerValue]+1];
            NSInteger integral = [self.strIntegral integerValue] - 100 * [self.numberLabel.text integerValue];
            self.integralLabel.text = [NSString stringWithFormat:@"%ld",integral];
            NSInteger amount = [self.strAmount integerValue] - [self.numberLabel.text integerValue];
            self.amountLabel.text = [NSString stringWithFormat:@"%ld",amount];
        }
        
    } else if (btn.tag == 101) {//-
        if ([self.numberLabel.text intValue] <= 0 ) {
            return;
        } else {
            self.numberLabel.text = [NSString stringWithFormat:@"%ld",[self.numberLabel.text integerValue]-1];
            NSInteger integral = [self.strIntegral integerValue] - 100 * [self.numberLabel.text integerValue];
            self.integralLabel.text = [NSString stringWithFormat:@"%ld",integral];
            NSInteger amount = [self.strAmount integerValue] - [self.numberLabel.text integerValue];
            self.amountLabel.text = [NSString stringWithFormat:@"%ld",amount];
        }
    }
}
- (void)payClick{
    if (self.payBlock) {
        self.payBlock(self.amountLabel.text);
    }
    [self dismissAlert];
}
- (void)dismissAlert {
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha -= 1;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
